<?php

/**
 * @version     $Id: view.html.php 3 2010-10-08 17:49:35Z obalyuk87@gmail.com $
 * @package     AdminSafe
 * @subpackage  Components
 * @copyright   Copyright (C) 2010 Oleksandr Balyuk (www.boolcast.com)
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL v2
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
// No direct access
defined('_JEXEC') or die('Restricted access');
require_once JPATH_COMPONENT_ADMINISTRATOR . DS . 'classes' . DS . 'component' . DS . 'view.php';

/**
 * Description of view
 *
 * @author Oleksandr Balyuk <obalyuk@boolcast.com>
 */
class AdminsafeViewInvalidkeylog extends AdminsafeView {

    protected $pagination = null;
    protected $log = null;
    protected $lists = null;
    protected $i = 0;
    protected $zeroOne = 0;
    protected $ajaxUrl = null;
    protected $banImg = null;
    protected $unbanImg = null;
    protected $warningImg = null;
    protected $errorImg = null;

    /**
     * Adds stylesheets and scripts
     */
    protected function _addStylesheetsAndScripts() {
        //
        JHTML::_('stylesheet', 'adminsafe.css', 'media/com_adminsafe/css/');

        //
        JHTML::_('script', 'adminsafe.js', 'media/com_adminsafe/js/');
    }

    /**
     * Loads Data
     */
    protected function _loadData() {
        $this->log = $this->get('Data');
        $this->pagination = $this->get('Pagination');
        $this->lists = $this->get('lists');
    }

    /**
     * Prepares data for display
     */
    protected function _prepareDataForDisplay() {

        //
        $token = JUtility::getToken();
        $this->ajaxUrl = JRoute::_("index.php?option=com_adminsafe&$token=1");
        $this->banImg = 'media/com_adminsafe/images/16/button_cancel.png';
        $this->unbanImg = 'media/com_adminsafe/images/16/button_ok.png';
        $this->warningImg = 'media/com_adminsafe/images/16/warning.png';
        $this->errorImg = 'media/com_adminsafe/images/16/error.png';

        //
        if ($this->log) {

            //
            $this->i = 0;
            foreach ($this->log as $item) {
                //
                $item->check = JHTML::_('grid.checkedout', $item, (int) $this->i++);

                //
                $item->ip_address = $this->_getBanButton($item->ip_address, $item->banned);
            }
        }
    }

    /**
     * Sets the toolbar
     */
    protected function _setToolBar() {
        JToolBarHelper::title(JText::_('CAS_HACK_ATTEMPTS'), 'warning');
        JToolBarHelper::deleteListX('Selected records will be erased permanently. Continue?');
        JToolBarHelper::spacer();
        $this->_addBackToCPanelButton();
    }

    /**
     * Gets Ban button
     *
     * @param string $ip
     * @param bool $isBunned
     * @return string
     */
    protected function _getBanButton($ip, $isBanned) {
        //
        $img = $isBanned ? $this->banImg : $this->unbanImg;
        $alt = $isBanned ? JText::_('Banned') : JText::_('Not Banned');

        //
        $id = uniqid('id_');
        $attribs = array('id' => "{$id}_img");
        $img = JHTML::image($img, $alt, $attribs);

        $text = ($isBanned) ? JText::_('Click to UNBAN IP') : JText::_('Click to BAN IP');
        $attribs = array(
            'title' => $text,
            'onclick' => "changeBanStatus('$id');",
            'id' => "{$id}_link"
        );

        $inputId = "<input type=\"hidden\" name=\"id\" value=\"$id\" />";
        $inputIp = "<input type=\"hidden\" id=\"{$id}_ip\" value=\"$ip\" />";
        $inputStat = "<input type=\"hidden\" id=\"{$id}_status\" value=\"$isBanned\" />";
        $waiter = "<span id=\"{$id}_waiter\" class=\"waiter\"><span class=\"ajax-loading-text\">Please Wait...</span></span>";
        $link = JHTML::link('#', "$ip $img", $attribs);

        return $inputId . $inputIp . $inputStat . $link . $waiter;
    }

}